package com.swinginwind.flexutils.components.timeComp.time.timeChooser
{
	/**
	 * 描述: 
	 * @版权所有: horizo
	 * @创建者: caoww
	 * @创建日期: 2011-1-24 
	 * @修改者: 
	 * @修改日期: 
	 * @修改说明: 
	 */
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.LinkButton;
	
	import spark.components.BorderContainer;
	import spark.components.Group;
	import spark.components.VGroup;

	[Event(name="change", type="flash.events.Event")]
	internal class DateGroup extends VGroup
	{
		public function DateGroup()
		{
			super();
		}
		public var selectedItem:uint
		private var _nowTime:Date;

		/**当前日期，用来生成日表*/
		public function get nowTime():Date
		{
			return _nowTime;
		}

		/**
		 * @private
		 */
		public function set nowTime(value:Date):void
		{
			_nowTime = value;
			if(value){
				selectedItem=value.getDate();
				this.invalidateProperties();				
			}
		}
		private var titleGroup:Group;
		private var mainGroup:Group;
		private var redBox:BorderContainer;
		override protected function createChildren():void{
			super.createChildren();
			this.percentHeight=100;
			this.percentWidth=100;
			this.gap=0;
			this.horizontalAlign="center";			
			titleGroup=new Group();
			titleGroup.width=MethodTimeChooser.LABELWIDTH*7;
			this.addElement(titleGroup);
			mainGroup=new Group();
			mainGroup.width=MethodTimeChooser.LABELWIDTH*7;
			mainGroup.height=MethodTimeChooser.LABELHEIGHT*6;
			this.addElement(mainGroup);
			createTitleList();
		}
		override protected function commitProperties():void{
			super.commitProperties();
			if(nowTime){
				createMainGroup(nowTime.getFullYear(),nowTime.getMonth()+1);	
			}
			
		}		
		private function createTitleList():void{
			//生成星期的标签
			var week:Array=MethodTimeChooser.createWeekData();
			for(var i:uint=0;i<week.length;i++){
				var labelbtn:LinkButton=MethodTimeChooser.createButton(week[i],true);
				labelbtn.mouseEnabled=false;
				labelbtn.x=MethodTimeChooser.LABELWIDTH*i;
				titleGroup.addElement(labelbtn);
			}
		}
		private function createMainGroup(YYYY:uint,MM:uint):void{
			var childNum:uint;
			var day31:Array=[1,3,5,7,8,10,12];
			var day30:Array=[4,6,9,11];
			if(MM==2){
				if(leapYear(YYYY)){
					childNum=29;
				}else{
					childNum=28;
				}
			}else{
				if(day31.indexOf(MM)>-1){
					childNum=31;
				}else if(day30.indexOf(MM)>-1){
					childNum=30;
				}				
			}
			//获取当月第一天的星期号
			var week:uint=new Date(YYYY,MM-1,1).getDay();
			//开始生成日期列表
			var column:uint=week;
			var row:uint=0;
			this.mainGroup.removeAllElements();
			for(var i:uint=1;i<=childNum;i++){
				var btn:LinkButton=MethodTimeChooser.createButton(String(i));
				btn.x=MethodTimeChooser.LABELWIDTH*column;
				btn.y=MethodTimeChooser.LABELHEIGHT*row;
				btn.addEventListener(MouseEvent.CLICK,function(e:MouseEvent):void{
					var currentBtn:LinkButton=e.currentTarget as LinkButton;
					var btnLabel:String=currentBtn.label;
					moveRedBox(currentBtn);
					selectedItem=uint(btnLabel);
					sendEvent();
				});
				if(i==this.selectedItem){
					this.moveRedBox(btn);
				}
				mainGroup.addElement(btn);	
				column++;
				if(column==7){
					column=0;
					row++;					
				}
			}
			mainGroup.addElement(redBox);
		}
		private function sendEvent():void{			
			dispatchEvent(new Event(Event.CHANGE));
		}
		private function moveRedBox(btn:LinkButton):void{
			if(!redBox)
				redBox=MethodTimeChooser.createRedBox();
			redBox.x=btn.x;
			redBox.y=btn.y;
			
		}
		//判读闰年
		private function leapYear(YYYY:uint):Boolean{
			if((YYYY%4)==0&&(YYYY%100)!=0){
				return true;
			}else if((YYYY%4)==0&&(YYYY%400)==0){
				return true;
			}
			
			return false;
		}
	}
}